package com.wwk.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.wwk.constant.MessageConstant;
import com.wwk.entity.PageResult;
import com.wwk.entity.QueryPageBean;
import com.wwk.entity.Result;
import com.wwk.pojo.CheckItem;
import com.wwk.service.CheckItemService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Classname 检查项管理
 * @Description TODO
 * @Date 2020/10/30 11:29
 * @Created by com.wwk
 * @Author com.wwk
 */
//@ResponseBody表示方法的返回值直接以指定的格式写入Http response body中，而不是解析为跳转路径
@RestController
@RequestMapping("/checkitem")
public class CheckItemController {

    @Reference
    private CheckItemService itemService;
    /*新增检查项*/
    @RequestMapping("/add")
    public Result add(@RequestBody CheckItem checkItem){
        //开发时，使用try-catch形式来判断是否执行成功
        try{
            itemService.add(checkItem);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }
        return new Result(true,MessageConstant.ADD_CHECKITEM_SUCCESS);
    }

    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean pageBean){
        //获取前端请求的数据包含，当前页，每页数和条件，调用服务来查询总数。
        final PageResult pageResult = itemService.findPage(pageBean);
        return pageResult;
    }

    //删除
    @PreAuthorize("hasAuthority('CHECKITEM_DELETE')")//权限校验
    @RequestMapping("/deleteById/{deleteId}")
    public Result delete(@PathVariable(value = "deleteId",required = true) Integer deleteId){
        try{
            itemService.deleteById(deleteId);
        }catch (RuntimeException e){
            //如果检查组包含删除的检查项，不能删除返回提示消息
            return new Result(false,e.getMessage());
        }catch (Exception e){
            //删除失败
            return new Result(false,MessageConstant.DELETE_CHECKITEM_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody CheckItem checkItem){
        System.out.println(checkItem);
        try{
            itemService.edit(checkItem);
        }catch (Exception e){
            e.printStackTrace();
            //编辑失败
            return new Result(false,MessageConstant.EDIT_CHECKITEM_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_CHECKITEM_SUCCESS);
    }

    @RequestMapping("/findById/{itemId}")
    public Result findById(@PathVariable(value = "itemId")Integer itemId){
        CheckItem checkItem=null;
        try{
            checkItem=itemService.findById(itemId);
        }catch (Exception e){
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
        return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItem);
    }
}
